<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * Xử lý dữ liệu user m_user
 *
 * @author luubk
 * @since Apr 9, 2012 - 1:50:50 PM
 */
class M_user extends MY_Model
{

    function __construct()
    {
        $this->table = 'user';        
        parent::__construct();
    }

    function _join()
    {
        $this->db->join('user_has_group', ' user.u_id = user_has_group.user_id', 'LEFT');
        $this->db->join('user_group', 'user_has_group.user_group_id = user_group.ug_id', 'LEFT');

        parent::_join();
    }

    /**
     * multi_select
     *
     * override the multi_select function
     * @param array $where where clause
     * @return FALSE if fail to query
     * 		result array if query successfully
     */
    function multi_select($where = array(), $order = FALSE, $limit = FALSE, $offset = 0)
    {
    	$this->db->select('user.u_id, user.u_name, user.u_email, user.u_created, user.u_updated, user.u_status
    			, group_concat(user_group.ug_name) AS u_groups, group_concat(user_group.ug_id) AS u_group_ids');
    	$this->db->from($this->table);

    	$this->_join();

    	if (!empty($where))
    		foreach ($where as $key => $val)
    		$this->db->where($key, $val);

    	$this->db->group_by('user.u_id,  user.u_name');

    	if ($order)
    		$this->db->order_by($order);

    	if ($limit)
    		$this->db->limit($limit, $offset);

    	$query = $this->db->get();

    	if ($query->num_rows() > 0)
    		return $query->result_array();
    	else
    		return FALSE;
    }

}

/**
 * End file m_user
 */
